#include "StrategyNode.h"

#include "StockNamespace.h"
#include "StockTypes.h"

#include "State.h"
#include "InitState.h"
#include "Visitor.h"


USING_STOCK_NS
using namespace std;

StrategyNode::StrategyNode(TransactionType transaction)
	: _transaction(transaction), _profit(0), _parent(NULL)
{
	_deal_state = InitState::GetInstance();
}

StrategyNode::~StrategyNode()
{

}

vector<StrategyNode *> StrategyNode::DoAvailableTransactions(StockPrice curPrice)
{
	vector<StrategyNode *> children;
	children = _deal_state->DoAvailableTransaction(this, curPrice);

	return children;
}

void StrategyNode::CalcProfit(StockProfit curProfit, StockPrice curPrice)
{
	if (_transaction == BUY)
	{
		_profit = curProfit - curPrice;
	}
	else if (_transaction == SELL)
	{
		_profit = curProfit + curPrice;
	}
	else if ((_transaction == PASS) || (_transaction == COOLDOWN))
	{
		_profit = curProfit;
	}
}

void StrategyNode::SetState(DealState * state)
{
	_deal_state = state;
}

void StrategyNode::AddChildrenNodes(vector<StrategyNode *> children)
{
	vector<StrategyNode *>::const_iterator it;

	for (it = children.begin(); it != children.end(); it++)
	{
		StrategyNode * child = (*it);

		_children.push_back(child);
		child->SetParent(this);
	}
}

vector<StrategyNode *> StrategyNode::GetChildren()
{
	return _children;
}

void StrategyNode::SetParent(StrategyNode * node)
{
	_parent = node;
}

StrategyNode * StrategyNode::GetParent()
{
	return _parent;
}

float StrategyNode::GetProfit()
{
	return _profit;
}

TransactionType StrategyNode::GetTransaction()
{
	return _transaction;
}

void StrategyNode::Accept(Visitor * visitor)
{
	visitor->Visit(this);

	vector<StrategyNode *> children = GetChildren();
	for (vector<StrategyNode *>::const_iterator it = children.begin(); it != children.end(); it++)
	{
		StrategyNode * node = (*it);
		node->Accept(visitor);
	}
}
